//
//  HZOrderQueryViewController.m
//  Stock_Stategy
//
//  Created by MayerF on 2018/1/24.
//  Copyright © 2018年 MayerF. All rights reserved.
//

#import "HZOrderQueryViewController.h"
#import "HZContionOrderView.h"

@interface HZOrderQueryViewController ()
@property (nonatomic, strong)HZContionOrderView *pOrderQuery;
@end

@implementation HZOrderQueryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的条件单";
    
    //关联逻辑管理对象
    self.logicManager = [HZBaseLogjcManager new];
    self.view.logicManager = self.logicManager;
    
    _pOrderQuery = [HZContionOrderView new];
    [self.view addSubview:_pOrderQuery];
    [_pOrderQuery mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    [self p_registerBlock];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [self p_sendRequestQueryOrderWithStatus:@"0"];
}

- (void)p_registerBlock
{
    __weak typeof (self)weakSelf = self;
    [self.logicManager mapLogic:kLogicRouterFinished_clickHeader toBlock:^id(NSDictionary *params, id other) {
        [weakSelf checkActivingRequestWithTitel:other];
        return nil;
    }];
    [self.logicManager mapLogic:kLogicRouterExcute_clickCancel toBlock:^id(NSDictionary *params, id other) {
        [weakSelf p_sendRequestCancelWithSerialNum:other];
        return nil;
    }];
}

- (void)checkActivingRequestWithTitel:(NSString *)title
{
    NSString *type = @"0";
    if ([title isEqual:@"生效中"]) {
        type = @"0";
    }else if ([title isEqual:@"已触发"]) {
        type = @"1";
    }else if ([title isEqual:@"已失效"]) {
        type = @"2";
    }
    [self p_sendRequestQueryOrderWithStatus:type];
}

- (void)p_sendRequestQueryOrderWithStatus:(NSString *)status
{
    [SVProgressHUD show];
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                          @"300041", kAPIFuctionKey,
                          @"",@"preprocess_id",
                          status,@"preprocess_status",
                          @"", @"preprocess_type",
                          @"", @"trigger_type",
                          nil];
    [HZApiManager apiPostParams:dict success:^(id objc) {
        NSMutableArray *tempArray = [NSMutableArray array];
        if([objc isKindOfClass:[NSArray class]]){
            for (NSDictionary *dict in objc) {
                HZOrderQueryModel *model = [HZOrderQueryModel mj_objectWithKeyValues:dict];
                [tempArray addObject:model];
            }
        }
        [_pOrderQuery updateNewData:tempArray];
        [SVProgressHUD dismiss];
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:error.domain];
    }];
}

- (void)p_sendRequestCancelWithSerialNum:(NSString *)serialNum
{
    [SVProgressHUD show];
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                          @"300042", kAPIFuctionKey,
                          serialNum,@"preprocess_id",
                          nil];
    [HZApiManager apiPostParams:dict success:^(id objc) {
        [SVProgressHUD showSuccessWithStatus:@"撤销成功"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self p_sendRequestQueryOrderWithStatus:0];
        });
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:error.domain];
    }];
}


@end
